package com.java.Servlet;

import com.java.entity.Orders;
import com.java.entity.Users;
import com.java.Exception.MyException;
import com.java.service.Impl.OrderServiceImpl;
import com.java.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {

    private OrderService service=new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String mark=req.getParameter("mark");
        
        if ("queryOrderById".equals(mark)){
            this.doQueryOrderById(req,resp);
        }else if ("queryOrderByBookId".equals(mark)){
            this.doQueryOrderByBookId(req,resp);
        }else if ("updateNameAndPhone".equals(mark)){
            this.doUpdateNameAndAndPhone(req,resp);
        }else if ("returnTicket".equals(mark)){
            this.doReturnTicket(req,resp);
        }

    }

    private void doReturnTicket(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String tui = req.getParameter("bid");//确认退票按钮
        System.out.println(tui);
        try {
            OrderService service=new OrderServiceImpl();
            List<String> bookIds = (List<String>) req.getSession().getAttribute("bookIds");
            if (bookIds==null){
                bookIds=new ArrayList<>();
            }
            bookIds.add(tui);
            req.getSession().setAttribute("bookIds",bookIds);
            doQueryOrderById(req,resp);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据bookId修改联系人和联系方式
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doUpdateNameAndAndPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

    }

    /**
     * 根据订单编号查询订单相关信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doQueryOrderByBookId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String bookId=req.getParameter("bookId");
        try {
            Orders orders=service.queryOrderByBookId(bookId);
            req.setAttribute("orders",orders);
            req.getRequestDispatcher("/user/test01/test2.jsp").forward(req,resp);
        } catch (MyException e) {
            e.printStackTrace();
        }

    }

    /**
 * 根据用户Id（身份证）查询订单
 */
    private void doQueryOrderById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Users users=(Users) req.getSession().getAttribute("user");

        try {
            List<Orders> orders=service.queryOrderById(users.getId());

            req.setAttribute("orders",orders);
            req.getRequestDispatcher("/user/test01/test1.jsp").forward(req,resp);

        } catch (MyException e) {
            e.printStackTrace();
        }
    }
}
